﻿//--------------------------------------------赵洪科----------------------------------------------//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.Security;

public partial class Member_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //获取用户身份的代码
        string EncrTicket = Request.Cookies[FormsAuthentication.FormsCookieName].Value;
        //获得身份验证票  
        FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(EncrTicket);
        //从身份验证票中提取经过验证的用户名  
        string UserName = ticket.Name;
        if (Session["car"] != null)
        {
            Label9.Visible = false;
            Label1.Visible = false;
            //从session["car"]中取得哈希表，与datalist绑定
            BookInfo books = new BookInfo();
            System.Collections.Hashtable hs = new Hashtable();
            Hashtable ht = (Hashtable)Session["car"];
            foreach (DictionaryEntry de in ht)
            {

                foreach (var book in books.GetBookNameById(Convert.ToInt64(de.Key)))
                {
                    hs[book.bookName] = ht[de.Key];
                }
            }
            DataList1.DataSource = hs;
            DataList1.DataBind();
        }
        else
        {
            Label9.Text = UserName;
            Panel1.Visible = false;
        }

    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {

    }

    //提交
    protected void Button1_Click(object sender, EventArgs e)
    {
        //获取用户身份的代码
        string EncrTicket = Request.Cookies[FormsAuthentication.FormsCookieName].Value;
        //获得身份验证票  
        FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(EncrTicket);
        //从身份验证票中提取经过验证的用户名  
        string UserName = ticket.Name;
        decimal price;   //由于获取商品的单价
        long bid;           //用于获取商品的id
        string name;    //用于获取书名
        Hashtable tempHt = (Hashtable)Session["car"];
        foreach (DictionaryEntry de in tempHt)
        {
            long tempId = Convert.ToInt64(de.Key);               //从session中获得书的ID号
            int tempNum = Convert.ToInt32(tempHt[de.Key]);     //从session中获得书的购买数量
            BookInfo book = new BookInfo();
            foreach (var b in book.GetBookNameById(tempId))
            {
                price = b.marketPrice;  //产品单价
                decimal totalpay = price * tempNum;  //总价钱

                bid = b.bookId;
                name = b.bookName;

                Members member = new Members();
                foreach (var mem in member.GetMembersByName(UserName))
                {
                    int id = mem.userId;


                    //提交后加入到orderlist表中，并且返回新插入记录的id（orderlistId）
                    OrderList orderlist = new OrderList();


                    //提交后加入到Orderdetail表中
                    OrderDetail orderdetail = new OrderDetail();
                    orderdetail.insertTo(orderlist.insertTo(id, UserName, TextBox3.Text, TextBox5.Text, totalpay), bid, name, price, 1);

                    //提交后加入到Transaction表中
                    Transaction trade = new Transaction();
                    trade.insertTo(totalpay,id, TextBox3.Text);

                }
                //提交后减少库存量
                for (; tempNum > 0; tempNum--)
                {
                    book.decreaseInventory(tempId);
                }
            }
        }
    }

    //清空购物车
    protected void Button2_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Redirect("~/Default.aspx");  //重新返回主页
    }
}